from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
import hashlib
import Ui_mainapp
import utility
import login
import pelanggan
import transaksi
import noawal
import nodok
import userData
import lapPelanggan

class MainApp(QMainWindow, Ui_mainapp.Ui_mainApp):  
  def __init__(self, parent=None):
        super(MainApp, self).__init__(parent)
        self.setupUi(self)        
        self.db = QSqlDatabase.addDatabase("QMYSQL")
#        self.db = QSqlDatabase.addDatabase("QODBC")
        self.connectDb()
        self.mdi = QWorkspace()
        self.setCentralWidget(self.mdi)
        self.showMaximized()
        self.formPelanggan  = None
        self.formTransaksi  = None
        self.formCredit  = None
        self.formDokNo  = None
        self.formUser  = None
        self.formLapPlg  = None
        self.dataGlob = {"kode":"", "nama":"", "grup":"", "level":""}                
        
  def connectDb(self):
        self.db.setDatabaseName("mmc")
        self.db = utility.Utility(self.db).setDBParm()
#        self.db.setHostName("localhost")
        self.db.setHostName("192.168.1.3")
        self.db.setPort(3306)
        if not self.db.open():
            QMessageBox.warning(None, "ConnectDB", QString("Database Error: %1").arg(self.db.lastError().text()))
            
  def actLogOut(self):
      self.dataGlob["kode"] = ""
      self.dataGlob["nama"] = ""
      self.dataGlob["grup"] = ""
      self.dataGlob["level"] = ""
      formlogin = login.Login(self)
      if formlogin.exec_():
          KODE_USER,  NAMA,  GRUP_MENU,  LEVEL_USER = range(4)
          nama = formlogin.userNameEdt.text()
          upass = formlogin.pwdEdt.text()    
          m = hashlib.md5()                 
          m.update(upass)
          query = QSqlQuery(self.db)
#          queryStr = "select kode_user, nama, grup_menu, level_user from master_user where kode_user = '"+nama+"' and pass_user = '"+upass+"' and stat_user = 'A'"
          queryStr = "select kode_user, nama, grup_menu, level_user from master_user where kode_user = '"+nama+"' and pass_user = '"+m.hexdigest()+"' and stat_user = 'A'"
          if self.db.isOpen():
              query.exec_(queryStr)
# ================== START - ini kalo pake driver mysql =============================
              if query.size() > 0:
                  while query.next():
                      self.dataGlob["kode"] = query.value(KODE_USER).toString()
                      self.dataGlob["nama"] = query.value(NAMA).toString()
                      self.dataGlob["grup"] = query.value(GRUP_MENU).toString()
                      self.dataGlob["level"] = query.value(LEVEL_USER).toString()
                  self.authApp()
                  formlogin.close()
              else:
                  QMessageBox.warning(None, "Login Error", "Wrong User Name / Password!")      
# ================== END - ini kalo pake driver mysql =============================

# ================== START - ini kalo pake driver odbc =============================
#              while query.next():
#                  self.dataGlob["kode"] = query.value(KODE_USER).toString()
#                  self.dataGlob["nama"] = query.value(NAMA).toString()
#                  self.dataGlob["grup"] = query.value(GRUP_MENU).toString()
#                  self.dataGlob["level"] = query.value(LEVEL_USER).toString()
#              self.authApp()
# ================== END - ini kalo pake driver odbc =============================

              if  self.dataGlob["kode"] == "":
                  QMessageBox.warning(None, "Login Error", "Wrong User Name / Password!")      
                  self.close()  
              else:
                  formlogin.close()
                
      else:
          self.close()    
        
  def actPelanggan(self):
      if self.formPelanggan is None:
        self.formPelanggan = pelanggan.Pelanggan(self.db, self.dataGlob,  self)
        self.mdi.addWindow(self.formPelanggan)
      self.formPelanggan.showMaximized()
      self.formPelanggan.show()
      
  def actTransaksi(self):
      if self.formTransaksi is None:
          self.formTransaksi = transaksi.Transaksi(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formTransaksi)
      self.formTransaksi.showMaximized()
      self.formTransaksi.show()
      
  def actCredit(self):
      if self.formCredit is None:
          self.formCredit = noawal.Noawal(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formCredit)
      self.formCredit.showMaximized()
      self.formCredit.show()
      
  def actDokNo(self):
      if self.formDokNo is None:
          self.formDokNo = nodok.Nodok(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formDokNo)
      self.formDokNo.showMaximized()
      self.formDokNo.show()
      
  def actUser(self):
      if self.formUser is None:
          self.formUser = userData.UserData(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formUser)
      self.formUser.showMaximized()
      self.formUser.show()
      
  def actLapPelanggan(self):
      if self.formLapPlg is None:
          self.formLapPlg = lapPelanggan.LapPelanggan(self.db, self.dataGlob, self)
          self.mdi.addWindow(self.formLapPlg)
      self.formLapPlg.showMaximized()
      self.formLapPlg.show()
      
  def authApp(self):
      self.actionStore.setEnabled(False)
      self.actionCredit_Card_No.setEnabled(False)
      self.actionDocument_No.setEnabled(False)
      self.actionUser.setEnabled(False)
      if self.dataGlob["level"] == "ADM":
          self.actionUser.setEnabled(True)
          self.actionStore.setEnabled(True)
          self.actionCredit_Card_No.setEnabled(True)
          self.actionDocument_No.setEnabled(True)
          
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = MainApp()
    form.connect(form.actionExit, SIGNAL("triggered()"), form.close)
    form.connect(form.actionLog_Out, SIGNAL("triggered()"), form.actLogOut)
    form.connect(form.actionPelanggan, SIGNAL("triggered()"), form.actPelanggan)
    form.connect(form.actionCredit_Card_No, SIGNAL("triggered()"), form.actCredit)
    form.connect(form.actionDocument_No, SIGNAL("triggered()"), form.actDokNo)
    form.connect(form.actionUser, SIGNAL("triggered()"), form.actUser)
    form.connect(form.actionLaporan_Pelanggan, SIGNAL("triggered()"), form.actLapPelanggan)
    
    form.connect(form.actionTransaksi, SIGNAL("triggered()"), form.actTransaksi)
    form.show()
    form.actLogOut()
    sys.exit(app.exec_())
